<!DOCTYPE html>

<html lang="en">
  <head>
    
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Model Behaviors &mdash; Phalcon 3.0.1 文档</title>
    <meta name="keywords" content="php, phalcon, phalcon php, php framework, faster php framework">
    <link rel="stylesheet" type="text/css" href="http://cdn.staticfile.org/twitter-bootstrap/3.3.6/css/bootstrap.min.css" />
    <link rel="stylesheet" type="text/css" href="/www/css/phalcon.min.css" />
    <link href='//fonts.googleapis.com/css?family=Open+Sans:700,400' rel='stylesheet' type='text/css'>
    <link href="//fonts.googleapis.com/css?family=Merriweather:400,700" rel="stylesheet" type="text/css" />
    <!--
    EUROPE <link href='//fonts.googleapis.com/css?family=Open+Sans:700,400&subset=latin-ext' rel='stylesheet' type='text/css'>
    GREEK <link href='//fonts.googleapis.com/css?family=Open+Sans:700,400&subset=greek-ext' rel='stylesheet' type='text/css'>
    RUSSIA <link href='//fonts.googleapis.com/css?family=Open+Sans:700,400&subset=cyrillic-ext,latin' rel='stylesheet' type='text/css'>
    -->

    <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
      <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

    <link rel="stylesheet" href="../static/pygments.css" type="text/css" />
    <link rel="stylesheet" href="../static/docs.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '3.0.1',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>

    <script src="http://cdn.staticfile.org/jquery/1.8.1/jquery.min.js"></script>
    <script type="text/javascript" src="../static/docs.js"></script>
    <link rel="shortcut icon" href="../static/favicon.ico"/>
    <link rel="top" title="Phalcon 3.0.1 文档" href="../index.html" />
    <link rel="next" title="模型元数据（Models Metadata）" href="models-metadata.html" />
    <link rel="prev" title="使用模型（Working with Models）" href="models.html" /> 
  </head>
  <body>

<header class="page-header">
    <nav class="navbar" role="navigation">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#main-menu-container">
                    <span class="sr-only">Toggle navigation</span><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span>
                </button>
                <a class="navbar-brand phalcon-logo" href="/"><span itemprop="name" class="sr-only">Phalcon PHP</span></a>
            </div>

            <div class="collapse navbar-collapse navbar-right" id="main-menu-container">
                <ul class="nav navbar-nav main-menu">
                  <li class="first"><a href="//phalconphp.com/en/download" class="header-nav-link">Download</a></li>
                  <li><a href="//docs.phalconphp.com/en/latest/index.html" class="header-nav-link" target="_blank">Documentation</a></li>
                  <li><a href="//forum.phalconphp.com/" class="header-nav-link" target="_blank">Forum</a></li>
                  <li><a href="//blog.phalconphp.com/" class="header-nav-link" target="_blank">Blog</a></li>
                  <li><a href="//phalconist.com/" class="header-nav-link" target="_blank">Resources</a></li>
                  <li><a href="//phalconphp.com/en/about">About</a></li>
                  <li><div align="right">
                      <iframe src="https://ghbtns.com/github-btn.html?user=phalcon&amp;repo=cphalcon&amp;type=watch&amp;count=true&amp;size=large"
      allowtransparency="true" frameborder="0" scrolling="0" width="152px" height="30px"></iframe>
                    </div></li>
                </ul>
            </div>
        </div>
    </nav>
  </header>

<div class="heading">
    <div class="container">
        <div class="row">
            <h2>Documentation</h2>
        </div>
    </div>
</div>
    <div class="related">
      <ul>
        <li class="right" >
          <a href="../genindex.html" title="总目录"
             accesskey="I">索引</a></li>
        <li class="right" >
          <a href="models-metadata.html" title="模型元数据（Models Metadata）"
             accesskey="N">下一页</a> |</li>
        <li class="right" >
          <a href="models.html" title="使用模型（Working with Models）"
             accesskey="P">上一页</a> |</li>
        <li><a href="//phalconphp.com">Home</a> &raquo;</li>
        <li><a href="../index.html">Phalcon 3.0.1 文档</a> &raquo;</li> 
      </ul>
    </div>  

      <table width="100%" align="center" cellpadding="0" cellspacing="0">
        <tr>
      <td class="primary-box" width="25%" valign="top">
            <div>
            <div id="searchbox" style="">
                <!--<form class="search" action="http://readthedocs.org/search/project/" method="get">
                  <input type="search" name="q" size="25" placeholder="Search">
                  <input type="submit" value="Go">
                  <input type="hidden" name="selected_facets" value="project:">
                </form>-->
                <div style="width:200px;padding:10px">
                  <gcse:searchbox-only></gcse:searchbox-only>
                </div>
            </div>
            </div>
            <div style="padding:5px;padding-left:10px">
              <div id="carbonads-container">
                <div class="carbonad"><div id="azcarbon"></div>
                <script async type="text/javascript" src="//cdn.carbonads.com/carbon.js?zoneid=1673&serve=C6AILKT&placement=phalconphpcom" id="_carbonads_js"></script>
                </div></div>
            </div>
            <h3><a href="../index.html">內容目录</a></h3>
            <ul>
<li><a class="reference internal" href="#">Model Behaviors</a><ul>
<li><a class="reference internal" href="#timestampable">生成时间戳（Timestampable）</a></li>
<li><a class="reference internal" href="#softdelete">软删除（SoftDelete）</a></li>
<li><a class="reference internal" href="#creating-your-own-behaviors">创建行为（Creating your own behaviors）</a></li>
<li><a class="reference internal" href="#traits-using-traits-as-behaviors">使用 Traits 实现行为(Using Traits as behaviors)</a></li>
</ul>
</li>
</ul>

            <h4>上一个主题</h4>
            <p class="topless"><a href="models.html" title="上一章">&lt; 使用模型（Working with Models）</a></p>
            <h4>下一个主题</h4>
            <p class="topless"><a href="models-metadata.html" title="下一章">模型元数据（Models Metadata） &gt;</a></p>
            <h3>本页</h3>
            <ul class="this-page-menu">
              <li><a href="../sources/reference/model-behaviors.md" rel="nofollow">显示源代码</a></li>
            </ul>
        </td>
          <td class="second-box" valign="top">
            <div class="document">
                <div class="documentwrapper">
                  <div class="bodywrapper">
                    <div class="body" >
                      
  <div class="section" id="model-behaviors">
<h1>Model Behaviors<a class="headerlink" href="#model-behaviors" title="永久链接至标题">¶</a></h1>
<p>Behaviors are shared conducts that several models may adopt in order to re-use code, the ORM provides an API to implement
behaviors in your models. Also, you can use the events and callbacks as seen before as an alternative to implement Behaviors with more freedom.</p>
<p>A behavior must be added in the model initializer, a model can have zero or more behaviors:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="k">use</span> <span class="nx">Phalcon\Mvc\Model</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Phalcon\Mvc\Model\Behavior\Timestampable</span><span class="p">;</span>

<span class="k">class</span> <span class="nc">Users</span> <span class="k">extends</span> <span class="nx">Model</span>
<span class="p">{</span>
    <span class="k">public</span> <span class="nv">$id</span><span class="p">;</span>

    <span class="k">public</span> <span class="nv">$name</span><span class="p">;</span>

    <span class="k">public</span> <span class="nv">$created_at</span><span class="p">;</span>

    <span class="k">public</span> <span class="k">function</span> <span class="nf">initialize</span><span class="p">()</span>
    <span class="p">{</span>
        <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">addBehavior</span><span class="p">(</span>
            <span class="k">new</span> <span class="nx">Timestampable</span><span class="p">(</span>
                <span class="p">[</span>
                    <span class="s2">&quot;beforeCreate&quot;</span> <span class="o">=&gt;</span> <span class="p">[</span>
                        <span class="s2">&quot;field&quot;</span>  <span class="o">=&gt;</span> <span class="s2">&quot;created_at&quot;</span><span class="p">,</span>
                        <span class="s2">&quot;format&quot;</span> <span class="o">=&gt;</span> <span class="s2">&quot;Y-m-d&quot;</span><span class="p">,</span>
                    <span class="p">]</span>
                <span class="p">]</span>
            <span class="p">)</span>
        <span class="p">);</span>
    <span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
<p>The following built-in behaviors are provided by the framework:</p>
<table border="1" class="docutils">
<colgroup>
<col width="11%" />
<col width="89%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Name</th>
<th class="head">Description</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>Timestampable</td>
<td>Allows to automatically update a model&#8217;s attribute saving the datetime when a record is created or updated</td>
</tr>
<tr class="row-odd"><td>SoftDelete</td>
<td>Instead of permanently delete a record it marks the record as deleted changing the value of a flag column</td>
</tr>
</tbody>
</table>
<div class="section" id="timestampable">
<h2>生成时间戳（Timestampable）<a class="headerlink" href="#timestampable" title="永久链接至标题">¶</a></h2>
<p>This behavior receives an array of options, the first level key must be an event name indicating when the column must be assigned:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="k">use</span> <span class="nx">Phalcon\Mvc\Model\Behavior\Timestampable</span><span class="p">;</span>

<span class="k">public</span> <span class="k">function</span> <span class="nf">initialize</span><span class="p">()</span>
<span class="p">{</span>
    <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">addBehavior</span><span class="p">(</span>
        <span class="k">new</span> <span class="nx">Timestampable</span><span class="p">(</span>
            <span class="p">[</span>
                <span class="s2">&quot;beforeCreate&quot;</span> <span class="o">=&gt;</span> <span class="p">[</span>
                    <span class="s2">&quot;field&quot;</span>  <span class="o">=&gt;</span> <span class="s2">&quot;created_at&quot;</span><span class="p">,</span>
                    <span class="s2">&quot;format&quot;</span> <span class="o">=&gt;</span> <span class="s2">&quot;Y-m-d&quot;</span><span class="p">,</span>
                <span class="p">]</span>
            <span class="p">]</span>
        <span class="p">)</span>
    <span class="p">);</span>
<span class="p">}</span>
</pre></div>
</div>
<p>Each event can have its own options, &#8216;field&#8217; is the name of the column that must be updated, if &#8216;format&#8217; is a string it will be used
as format of the PHP&#8217;s function <a class="reference external" href="http://php.net/manual/zh/function.date.php">date</a>, format can also be an anonymous function providing you the free to generate any kind timestamp:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="k">use</span> <span class="nx">DateTime</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">DateTimeZone</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Phalcon\Mvc\Model\Behavior\Timestampable</span><span class="p">;</span>

<span class="k">public</span> <span class="k">function</span> <span class="nf">initialize</span><span class="p">()</span>
<span class="p">{</span>
    <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">addBehavior</span><span class="p">(</span>
        <span class="k">new</span> <span class="nx">Timestampable</span><span class="p">(</span>
            <span class="p">[</span>
                <span class="s2">&quot;beforeCreate&quot;</span> <span class="o">=&gt;</span> <span class="p">[</span>
                    <span class="s2">&quot;field&quot;</span>  <span class="o">=&gt;</span> <span class="s2">&quot;created_at&quot;</span><span class="p">,</span>
                    <span class="s2">&quot;format&quot;</span> <span class="o">=&gt;</span> <span class="k">function</span> <span class="p">()</span> <span class="p">{</span>
                        <span class="nv">$datetime</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Datetime</span><span class="p">(</span>
                            <span class="k">new</span> <span class="nx">DateTimeZone</span><span class="p">(</span><span class="s2">&quot;Europe/Stockholm&quot;</span><span class="p">)</span>
                        <span class="p">);</span>

                        <span class="k">return</span> <span class="nv">$datetime</span><span class="o">-&gt;</span><span class="na">format</span><span class="p">(</span><span class="s2">&quot;Y-m-d H:i:sP&quot;</span><span class="p">);</span>
                    <span class="p">}</span>
                <span class="p">]</span>
            <span class="p">]</span>
        <span class="p">)</span>
    <span class="p">);</span>
<span class="p">}</span>
</pre></div>
</div>
<p>If the option &#8216;format&#8217; is omitted a timestamp using the PHP&#8217;s function <a class="reference external" href="http://php.net/manual/zh/function.time.php">time</a>, will be used.</p>
</div>
<div class="section" id="softdelete">
<h2>软删除（SoftDelete）<a class="headerlink" href="#softdelete" title="永久链接至标题">¶</a></h2>
<p>This behavior can be used in the following way:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="k">use</span> <span class="nx">Phalcon\Mvc\Model</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Phalcon\Mvc\Model\Behavior\SoftDelete</span><span class="p">;</span>

<span class="k">class</span> <span class="nc">Users</span> <span class="k">extends</span> <span class="nx">Model</span>
<span class="p">{</span>
    <span class="k">const</span> <span class="no">DELETED</span> <span class="o">=</span> <span class="s2">&quot;D&quot;</span><span class="p">;</span>

    <span class="k">const</span> <span class="no">NOT_DELETED</span> <span class="o">=</span> <span class="s2">&quot;N&quot;</span><span class="p">;</span>



    <span class="k">public</span> <span class="nv">$id</span><span class="p">;</span>

    <span class="k">public</span> <span class="nv">$name</span><span class="p">;</span>

    <span class="k">public</span> <span class="nv">$status</span><span class="p">;</span>



    <span class="k">public</span> <span class="k">function</span> <span class="nf">initialize</span><span class="p">()</span>
    <span class="p">{</span>
        <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">addBehavior</span><span class="p">(</span>
            <span class="k">new</span> <span class="nx">SoftDelete</span><span class="p">(</span>
                <span class="p">[</span>
                    <span class="s2">&quot;field&quot;</span> <span class="o">=&gt;</span> <span class="s2">&quot;status&quot;</span><span class="p">,</span>
                    <span class="s2">&quot;value&quot;</span> <span class="o">=&gt;</span> <span class="nx">Users</span><span class="o">::</span><span class="na">DELETED</span><span class="p">,</span>
                <span class="p">]</span>
            <span class="p">)</span>
        <span class="p">);</span>
    <span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
<p>This behavior accepts two options: &#8216;field&#8217; and &#8216;value&#8217;, &#8216;field&#8217; determines what field must be updated and &#8216;value&#8217; the value to be deleted.
Let&#8217;s pretend the table &#8216;users&#8217; has the following data:</p>
<div class="highlight-bash"><div class="highlight"><pre>mysql&gt; <span class="k">select</span> * from users<span class="p">;</span>
+----+---------+--------+
<span class="p">|</span> id <span class="p">|</span> name    <span class="p">|</span> status <span class="p">|</span>
+----+---------+--------+
<span class="p">|</span>  <span class="m">1</span> <span class="p">|</span> Lana    <span class="p">|</span> N      <span class="p">|</span>
<span class="p">|</span>  <span class="m">2</span> <span class="p">|</span> Brandon <span class="p">|</span> N      <span class="p">|</span>
+----+---------+--------+
<span class="m">2</span> rows in <span class="nb">set</span> <span class="o">(</span>0.00 sec<span class="o">)</span>
</pre></div>
</div>
<p>If we delete any of the two records the status will be updated instead of delete the record:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="nx">Users</span><span class="o">::</span><span class="na">findFirst</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span><span class="o">-&gt;</span><span class="na">delete</span><span class="p">();</span>
</pre></div>
</div>
<p>The operation will result in the following data in the table:</p>
<div class="highlight-bash"><div class="highlight"><pre>mysql&gt; <span class="k">select</span> * from users<span class="p">;</span>
+----+---------+--------+
<span class="p">|</span> id <span class="p">|</span> name    <span class="p">|</span> status <span class="p">|</span>
+----+---------+--------+
<span class="p">|</span>  <span class="m">1</span> <span class="p">|</span> Lana    <span class="p">|</span> N      <span class="p">|</span>
<span class="p">|</span>  <span class="m">2</span> <span class="p">|</span> Brandon <span class="p">|</span> D      <span class="p">|</span>
+----+---------+--------+
<span class="m">2</span> rows in <span class="nb">set</span> <span class="o">(</span>0.01 sec<span class="o">)</span>
</pre></div>
</div>
<p>Note that you need to specify the deleted condition in your queries to effectively ignore them as deleted records, this behavior doesn&#8217;t support that.</p>
</div>
<div class="section" id="creating-your-own-behaviors">
<h2>创建行为（Creating your own behaviors）<a class="headerlink" href="#creating-your-own-behaviors" title="永久链接至标题">¶</a></h2>
<p>The ORM provides an API to create your own behaviors. A behavior must be a class implementing the <a class="reference internal" href="../api/Phalcon_Mvc_Model_BehaviorInterface.html"><em>Phalcon\Mvc\Model\BehaviorInterface</em></a>.
Also, <a class="reference internal" href="../api/Phalcon_Mvc_Model_Behavior.html"><em>Phalcon\Mvc\Model\Behavior</em></a> provides most of the methods needed to ease the implementation of behaviors.</p>
<p>The following behavior is an example, it implements the Blameable behavior which helps identify the user
that is performed operations over a model:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="k">use</span> <span class="nx">Phalcon\Mvc\Model\Behavior</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Phalcon\Mvc\Model\BehaviorInterface</span><span class="p">;</span>

<span class="k">class</span> <span class="nc">Blameable</span> <span class="k">extends</span> <span class="nx">Behavior</span> <span class="k">implements</span> <span class="nx">BehaviorInterface</span>
<span class="p">{</span>
    <span class="k">public</span> <span class="k">function</span> <span class="nf">notify</span><span class="p">(</span><span class="nv">$eventType</span><span class="p">,</span> <span class="nv">$model</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="k">switch</span> <span class="p">(</span><span class="nv">$eventType</span><span class="p">)</span> <span class="p">{</span>

            <span class="k">case</span> <span class="s2">&quot;afterCreate&quot;</span><span class="o">:</span>
            <span class="k">case</span> <span class="s2">&quot;afterDelete&quot;</span><span class="o">:</span>
            <span class="k">case</span> <span class="s2">&quot;afterUpdate&quot;</span><span class="o">:</span>

                <span class="nv">$userName</span> <span class="o">=</span> <span class="c1">// ... get the current user from session</span>

                <span class="c1">// Store in a log the username, event type and primary key</span>
                <span class="nb">file_put_contents</span><span class="p">(</span>
                    <span class="s2">&quot;logs/blamable-log.md&quot;</span><span class="p">,</span>
                    <span class="nv">$userName</span> <span class="o">.</span> <span class="s2">&quot; &quot;</span> <span class="o">.</span> <span class="nv">$eventType</span> <span class="o">.</span> <span class="s2">&quot; &quot;</span> <span class="o">.</span> <span class="nv">$model</span><span class="o">-&gt;</span><span class="na">id</span>
                <span class="p">);</span>

                <span class="k">break</span><span class="p">;</span>

            <span class="k">default</span><span class="o">:</span>
                <span class="cm">/* ignore the rest of events */</span>
        <span class="p">}</span>
    <span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
<p>The former is a very simple behavior, but it illustrates how to create a behavior, now let&#8217;s add this behavior to a model:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="k">use</span> <span class="nx">Phalcon\Mvc\Model</span><span class="p">;</span>

<span class="k">class</span> <span class="nc">Profiles</span> <span class="k">extends</span> <span class="nx">Model</span>
<span class="p">{</span>
    <span class="k">public</span> <span class="k">function</span> <span class="nf">initialize</span><span class="p">()</span>
    <span class="p">{</span>
        <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">addBehavior</span><span class="p">(</span>
            <span class="k">new</span> <span class="nx">Blameable</span><span class="p">()</span>
        <span class="p">);</span>
    <span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
<p>A behavior is also capable of intercepting missing methods on your models:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="k">use</span> <span class="nx">Phalcon\Tag</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Phalcon\Mvc\Model\Behavior</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Phalcon\Mvc\Model\BehaviorInterface</span><span class="p">;</span>

<span class="k">class</span> <span class="nc">Sluggable</span> <span class="k">extends</span> <span class="nx">Behavior</span> <span class="k">implements</span> <span class="nx">BehaviorInterface</span>
<span class="p">{</span>
    <span class="k">public</span> <span class="k">function</span> <span class="nf">missingMethod</span><span class="p">(</span><span class="nv">$model</span><span class="p">,</span> <span class="nv">$method</span><span class="p">,</span> <span class="nv">$arguments</span> <span class="o">=</span> <span class="p">[])</span>
    <span class="p">{</span>
        <span class="c1">// If the method is &#39;getSlug&#39; convert the title</span>
        <span class="k">if</span> <span class="p">(</span><span class="nv">$method</span> <span class="o">==</span> <span class="s2">&quot;getSlug&quot;</span><span class="p">)</span> <span class="p">{</span>
            <span class="k">return</span> <span class="nx">Tag</span><span class="o">::</span><span class="na">friendlyTitle</span><span class="p">(</span><span class="nv">$model</span><span class="o">-&gt;</span><span class="na">title</span><span class="p">);</span>
        <span class="p">}</span>
    <span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
<p>Call that method on a model that implements Sluggable returns a SEO friendly title:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="nv">$title</span> <span class="o">=</span> <span class="nv">$post</span><span class="o">-&gt;</span><span class="na">getSlug</span><span class="p">();</span>
</pre></div>
</div>
</div>
<div class="section" id="traits-using-traits-as-behaviors">
<h2>使用 Traits 实现行为(Using Traits as behaviors)<a class="headerlink" href="#traits-using-traits-as-behaviors" title="永久链接至标题">¶</a></h2>
<p>Starting from PHP 5.4 you can use <a class="reference external" href="http://php.net/manual/zh/language.oop5.traits.php">Traits</a> to re-use code in your classes, this is another way to implement
custom behaviors. The following trait implements a simple version of the Timestampable behavior:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="k">trait</span> <span class="nx">MyTimestampable</span>
<span class="p">{</span>
    <span class="k">public</span> <span class="k">function</span> <span class="nf">beforeCreate</span><span class="p">()</span>
    <span class="p">{</span>
        <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">created_at</span> <span class="o">=</span> <span class="nb">date</span><span class="p">(</span><span class="s2">&quot;r&quot;</span><span class="p">);</span>
    <span class="p">}</span>

    <span class="k">public</span> <span class="k">function</span> <span class="nf">beforeUpdate</span><span class="p">()</span>
    <span class="p">{</span>
        <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">updated_at</span> <span class="o">=</span> <span class="nb">date</span><span class="p">(</span><span class="s2">&quot;r&quot;</span><span class="p">);</span>
    <span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
<p>Then you can use it in your model as follows:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="k">use</span> <span class="nx">Phalcon\Mvc\Model</span><span class="p">;</span>

<span class="k">class</span> <span class="nc">Products</span> <span class="k">extends</span> <span class="nx">Model</span>
<span class="p">{</span>
    <span class="k">use</span> <span class="nx">MyTimestampable</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
</div>
</div>
</div>


                    </div>
                  </div>
                </div>
            </div>
          </td>
        </tr>
      </table>
    <div class="related">
      <ul>
        <li class="right" >
          <a href="../genindex.html" title="总目录"
             >索引</a></li>
        <li class="right" >
          <a href="models-metadata.html" title="模型元数据（Models Metadata）"
             >下一页</a> |</li>
        <li class="right" >
          <a href="models.html" title="使用模型（Working with Models）"
             >上一页</a> |</li> 
      </ul>
    </div>

      <div class="prefooter">
  <div class="container">
      <div class="row">
          <div class="col-sm-3 text-right">
              <span>Follow along:</span>
          </div>
          <div class="col-sm-6 text-center">
              <a href="https://twitter.com/phalconphp" alt="Twitter" class="btn-social btn-social-twitter"><i class="icon-twitter"></i></a>
              <a href="https://www.facebook.com/pages/Phalcon-Framework/134230726685897" alt="Facebook" class="btn-social btn-social-facebook"><i class="icon-facebook"></i></a>
              <a href="https://plus.google.com/102376109340560896457" alt="Google+" class="btn-social btn-social-googleplus"><i class="icon-googleplus"></i></a>
              <a href="https://github.com/phalcon/cphalcon" alt="Github" class="btn-social btn-social-github"><i class="icon-github"></i></a>
          </div>
          <div class="col-sm-3">
          </div>
      </div>
  </div>

</div>
<footer class="footer">
    <div class="container">
        <div class="row">
            <div class="col-xs-4 col-sm-3">
                <h4>Download</h4>
                <ul>
                    <li><a href="/download">Installing Phalcon PHP</a></li>
                    <li><a href="http://docs.phalconphp.com/en/latest/index.html" class="header-nav-link" target="_blank">Documentation</a></li>
                    <li><a href="http://api.phalconphp.com">API</a></li>
                    <li><a href="http://docs.phalconphp.com/en/latest/reference/tutorial.html">Tutorial</a></li>
                    <li><a href="http://docs.phalconphp.com/en/latest/reference/tutorial.html#sample-applications">Sample Applications</a></li>
                </ul>
            </div>
            <div class="col-xs-4 col-sm-3">
                <h4>Community</h4>
                <ul>
                    <li><a href="http://forum.phalconphp.com/" class="header-nav-link" target="_blank">Forum</a></li>
                    <li><a href="https://github.com/phalcon/cphalcon">GitHub</a></li>
                    <li><a href="https://github.com/phalcon/cphalcon/issues">Issue Tracker</a></li>
                    <li><a href="http://stackoverflow.com/questions/tagged/phalcon">Stack Overflow</a></li>
                    <li><a href="/en/testimonials">Testimonials</a></li>
                    <li><a href="http://builtwith.phalconphp.com/">Built with Phalcon</a></li>
                    <li><a href="http://store.phalconphp.com/">Store</a></li>
                </ul>
            </div>
            <div class="col-xs-4 col-sm-2">
                <h4>About</h4>
                <ul>
                    <li><a class="link-black" href="http://blog.phalconphp.com/">Blog</a></li>
                    <li><a href="/en/about">About</a></li>
                    <li><a href="/en/team">Team</a></li>
                    <li><a href="/en/roadmap">Roadmap</a></li>
                    <li><a href="/en/donate">Donate</a></li>
                    <li><a href="/en/consulting">Consulting</a></li>
                    <li><a href="/en/hosting">Hosting</a></li>
                </ul>
            </div>
            <div id="license-spaccer" class="visible-xs"></div>
            <div id="license-wrapper" class="col-xs-12 col-sm-4">
                <p class="license">
                    Phalcon is an open-source PHP framework <br>built as a C-extension. It is available under the <br>
                    <a href="http://opensource.org/licenses/BSD-3-Clause" target="_blank">new BSD License</a>.
                    <br>
                    <br>
                    Except where otherwise noted, content on this site is licensed under the
                    <a href="http://creativecommons.org/licenses/by/3.0/">Creative Commons Attribution 3.0 License.</a>
                </p>
            </div>
        </div>
    </div>
</footer>

    </div>
    <script type="text/javascript">
    $(window).on("load", function(){
      var cx = '009733439235723428699:lh9ltjgvdz8';
      var gcse = document.createElement('script');
      gcse.type = 'text/javascript';
      gcse.async = true;
      gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//www.google.com/cse/cse.js?cx=' + cx;
      var s = document.getElementsByTagName('script')[0];
      s.parentNode.insertBefore(gcse, s);
    });
    </script>

  </body>
</html>